"""
Case Type   : 系统内部使用工具
Case Name   : 创建范围分区表
Create At   : 2022/03/03
Owner       : zou_jialiang050
Description :
    1.构建数据
    2.清理环境
Expect      :
    1.构建成功
    2. 成功
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.Common import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

Constant = Constant()


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pricomsh = CommonSH("PrimaryDbUser")
        self.primary_node = Node('PrimaryDbUser')
        self.target_path = os.path.join(macro.DB_BACKUP_PATH, "source_data")

    def test_ustore(self):
        text = f"----step1:创建范围分区表:expect:成功;----"
        self.log.info(text)
        file_lis = ['tab_ap_join_06.txt']
        for file in file_lis:
            self.common.scp_file(self.primary_node, f"{file}", self.target_path)

        sql1 = f"""drop table if exists tab_ap_join_06;
        create table tab_ap_join_06 ( 
                                c_id int , 
                                c_d_id char(20) , 
                                c_w_id int, 
                                c_first varchar(16) , 
                                c_middle char(2) , 
                                c_last varchar(16) , 
                                c_street_1 varchar(2000) , 
                                c_street_2 varchar(20) , 
                                c_city varchar(20) , 
                                c_state char(2) , 
                                c_zip char(9) , 
                                c_phone char(16) , 
                                c_since varchar(20) , 
                                c_credit char(2) , 
                                c_credit_lim numeric(12,2) , 
                                c_discount decimal , 
                                c_balance numeric(12,2) , 
                                c_ytd_payment varchar(20) , 
                                c_payment_cnt int , 
                                c_delivery_cnt int , 
                                c_data varchar(500)) 
        with (storage_type=ustore) partition by range (c_id)
        (
        partition tab_ap_join_06_1 values less than (10),
        partition tab_ap_join_06_2 values less than (77),
        partition tab_ap_join_06_3 values less than (337),
        partition tab_ap_join_06_4 values less than (573),
        partition tab_ap_join_06_5 values less than (1357),
        partition tab_ap_join_06_6 values less than (2033),
        partition tab_ap_join_06_7 values less than (2087),
        partition tab_ap_join_06_8 values less than (2387),
        partition tab_ap_join_06_9 values less than (2687),
        partition tab_ap_join_06_10 values less than (2987),
        partition tab_ap_join_06_11 values less than (maxvalue)
        );
        
        copy tab_ap_join_06 from 
        '{os.path.join(self.target_path, 'tab_ap_join_06.txt')}' delimiters  ',';
        """

        res1 = self.pricomsh.execut_db_sql(sql1)
        self.log.info(res1)

        self.assertIn('CREATE TABLE', res1, 'sql1执行失败')
        self.assertIn('COPY 3000', res1, 'sql1执行失败')

    def tearDown(self):
        text = f"----step2:清理环境:expect:成功;----"
        self.log.info(text)
        del_flag = False
        del_cmd = f'rm -rf {self.target_path};' \
                  f'if [ -d {self.target_path} ]; ' \
                  f'then echo "exists";else echo "not exists"; fi'
        for num in range(10):
            del_res = self.common.get_sh_result(self.primary_node, del_cmd)
            if 'not exists' in del_res:
                del_flag = True
                break
        self.assertTrue(del_flag, '文件删除失败')
        self.log.info(f'----{os.path.basename(__file__)} finish----')
